using System.Linq;
using System.Collections.Generic;
using Managistics.Model;
using NHibernate.Criterion;
using NHibernate.Linq;

namespace Managistics.Core.Repositories.Impl
{
    public class InputSheetDetailRepository : RepositoryBase<InputSheetDetail, double>, IInputSheetDetailRepository
    {
        /// <summary>
        /// Select Details by InputSheetId
        /// </summary>
        /// <param name="inputSheetId"></param>
        /// <returns></returns>
        public List<InputSheetDetail> SelectDetailsByInputSheetId(string inputSheetId)
        {
            var query = (from inputDetail in this.Session.Query<InputSheetDetail>()
                         join input in this.Session.Query<InputSheet>() on inputDetail.InputSheetId equals input.InputSheetId
                         join good in this.Session.Query<Good>() on inputDetail.ItemId equals good.Code
                         where inputDetail.InputSheetId == inputSheetId
                         select new InputSheetDetail
                         {
                             InputSheet = input,
                             Good = good
                         });

            return query.ToList<InputSheetDetail>();
        }

        /// <summary>
        /// Select by InputSheetId
        /// </summary>
        /// <param name="inputSheetId"></param>
        /// <returns></returns>
        public IList<InputSheetDetail> SelectByISheet(string inputSheetId)
        {
            return this.Session.QueryOver<InputSheetDetail>()
                .Where(x => x.InputSheetId == inputSheetId)
                .List<InputSheetDetail>();
        }

        /// <summary>
        /// Get by InputSheetId and GoodCode
        /// </summary>
        /// <param name="InputSheetId"></param>
        /// <param name="GoodCode"></param>
        /// <returns></returns>
        public InputSheetDetail GetByKey(string InputSheetId, string GoodCode)
        {
            return this.Session.QueryOver<InputSheetDetail>()
                .Where(x => x.InputSheetId == InputSheetId)
                .Where(x => x.ItemId == GoodCode)
                .SingleOrDefault<InputSheetDetail>();
        }
                
    }

}